package com.songlanyun.common.jushuitan;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jushuitan.api.ApiClient;
import com.jushuitan.api.ApiRequest;
import com.jushuitan.api.ApiResponse;
import com.jushuitan.api.DefaultApiClient;
import com.jushuitan.api.exception.ApiException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.songlanyun.common.utils.R;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.spring.web.json.Json;

import javax.xml.crypto.Data;
import java.io.IOException;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeoutException;


/**
 * 首页
 *
 * @author hd
 */
@RestController
//@RunWith(SpringRunner.class)
@RequestMapping("/jst")
public class JushuitanController {
    /** 系统基础配置 */
    @Autowired
    private RabbitTemplate rabbitTemplate;


    public R send(){
        // 实例化client
        ApiClient client = new DefaultApiClient();
        // 设置超时时间
//        ((DefaultApiClient)client).setReadTimeout(3000);
//        ((DefaultApiClient)client).setConnectTimeout(2000);
        // 替换为POP分配给应用的app_key,app_secret,accessToken等
        String url = "https://openapi.jushuitan.com/open/jushuitan/orders/upload";// 示例为测试地址，生产上请替换为正式地址
        String appKey = "d3e46effa5ad41f0a19b17e1066bc863";
        String appSecret = "1704ebbd7c114509941dfafcf2f92483";
        String accessToken = "57bb112e47ac4a0f97f33491163cea59";
//        String biz = "{\"shop_ids\":[],\"page_index\":1,\"page_size\":30}";
        String biz =  getJsonData();
        // 构建请求对象
        ApiRequest request = new ApiRequest.Builder(url, appKey, appSecret)
                .biz(biz).build();
        System.out.println("biz"+biz);

        // 执行接口调用
        try {
            ApiResponse response = client.execute(request, accessToken);
            System.out.println("is success: " + response.isSuccess());
            System.out.println("body: " + response.getBody());
        } catch (ApiException e) {
            switch (e.getCodeEnum()) {
                case SYS_ERROR:
                    // TODO retry
                    break;
                case PARAM_ERROR:
                    // TODO check param
                    break;
                case SIGN_ERROR:
                    // TODO @see com.jushuitan.api.util.ApiUtil
                    break;
                case CONNECT_ERROR:
                    // TODO check network
                    break;
                case TIME_OUT_ERROR:
                    // TODO check logic or reset readTimeout
                    break;
                case EXECUTE_ERROR:
                    // TODO @see com.jushuitan.api.DefaultApiClient#executeRemote
                    break;
                default:
                    // log
                    break;
            }
        } catch (Exception e) {
            System.out.println("error:"+e);
            // exception handle
        }
        return  R.ok();
    }


    String getJsonData(){
        //https://openweb.jushuitan.com/dev-doc?docType=4&docId=18#请求示例
        JSONObject jsonObject = new JSONObject();

        jsonObject.put("shop_id",15951789);    //店铺id
        Date date1 = new Date();
        long time = date1.getTime();
        jsonObject.put("so_id",String.valueOf(time));     //自研商城系统订单号,长度<=50 唯一值
        jsonObject.put("order_date","2024-03-25 18:14:26");  //订单日期
        jsonObject.put("shop_status","WAIT_SELLER_SEND_GOODS");   //商城订单状态
        jsonObject.put("wms_co_id",13775363);   //一链金服云仓  发货仓编号  固定
        jsonObject.put("shop_buyer_id","1163");   //买家账号  id
        jsonObject.put("receiver_state","辽宁省");   //省
        jsonObject.put("receiver_city","沈阳市");    //市
        jsonObject.put("receiver_district","于洪区");   //区
        jsonObject.put("receiver_address","详细地址");    //详细地址
        jsonObject.put("receiver_name","胡冬");    //收货人姓名
        jsonObject.put("receiver_phone","15142066403");    //收货人电话
        jsonObject.put("pay_amount",14.50);    //应付金额  小数两位
        jsonObject.put("freight",0);    //运费

        JSONObject itemJson = new JSONObject();   //商品明细
        JSONArray itemArray = new JSONArray();
        itemJson.put("sku_id","457325851058556");    //商品编码
        itemJson.put("shop_sku_id","123");    //商品编码  id
        itemJson.put("amount",14.50);    //商品金额  两位小数
        itemJson.put("base_price",14.50);    //商品原价  两位小数
        itemJson.put("name","鲜肌之谜多肽蛋白精华乳");    //商品名称
        Random rand = new Random();
        int randomNumber = 10000+rand.nextInt(99999);
        String randomNumberString = String.valueOf(randomNumber).substring(0, 5);
        int randomNumber2 = 10000+rand.nextInt(99999);
        String randomNumberString2 = String.valueOf(randomNumber2).substring(0, 5);
        itemJson.put("outer_oi_id",randomNumberString+"-"+randomNumberString2);    //商品子订单id
        itemJson.put("qty",1);    //商品数量
        itemArray.add(itemJson);
        jsonObject.put("items",itemArray);

        JSONObject payJson = new JSONObject();    //支付信息
        payJson.put("outer_pay_id",String.valueOf(10000+rand.nextInt(999999999)).substring(0,9));    //支付单号
        payJson.put("pay_date","2024-03-25 18:14:30");    //支付日期
        payJson.put("payment","point");    //支付方式
        payJson.put("seller_account","xxxxxx");    //支付账号
        payJson.put("buyer_account","yyyyyy");    //买家账号
        payJson.put("amount",12.00);    //支付金额  两位小数
        jsonObject.put("pay",payJson);

        JSONArray result = new JSONArray();
        result.add(jsonObject);
        return result.toJSONString();
    }


}
